import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  build: {
    target: 'es2017',
    minify: 'terser',
    manifest: false,
    sourcemap: false,
    outDir: 'dist',
  },
  server: {
    open: true,   // 热启动
    port: Number(process.env.VITE_CLI_PORT),   // 端口号
    proxy: {
      [process.env.VITE_PROXY_API as string]: {
        target: process.env.VITE_BASE_PATH as string,
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp(`^${process.env.VITE_PROXY_API}`), '')
      },
      '/sse': {
        target: `${process.env.VITE_BASE_PATH}`,
        changeOrigin: true,
        rewrite: (path) => path.replace(new RegExp('^/sse'), '')
      }
    }
  }
})
